import 'dart:convert';
import 'dart:ffi';

import 'package:flutter/services.dart';
import 'package:jetour_line/aha_jetour_line.dart';
import 'package:jetour_line/model/location_info_model.dart';
import 'package:jetour_line/network/aha_http.dart';

import '../base_widget/my_alert.dart';

class MapUtils{
  static LocationInfoModel? locationInfoModel;
  static List<String> hasMaps = [];
  // ignore: constant_identifier_names
  static const String MAP_PLUGIN = "amap_flutter_plugin";
  // ignore: constant_identifier_names
  static const String MAP_STREAM_PLUGIN = "amap_flutter_plugin_stream";

  static const MethodChannel _methodChannel = MethodChannel(MAP_PLUGIN);

  static const EventChannel _eventChannel = EventChannel(MAP_STREAM_PLUGIN);


  static final Stream<Map<String, Object>> _onLocationChanged = _eventChannel
      .receiveBroadcastStream()
      .asBroadcastStream()
      .map<Map<String, Object>>((element) => element.cast<String, Object>());

  static void setKey(String adnridKey,String iosKey){
    Map<String ,String> msg = Map();
    msg['android'] = adnridKey;
    msg['ios'] = iosKey;
    _methodChannel.invokeMethod('set_map_key',msg);
  }

  static void getLocation(Function callBack){
    _methodChannel.invokeMethod('startLocation');
    onLocationChanged(callBack);
  }
  ///定位结果回调
  static void onLocationChanged(Function callBack) {
      _onLocationChanged.listen((Map<String, Object> event) {
        if (event['method'] == 'startLocation') {
          double? lng = event['lng'] as double;
          double? lat = event['lat']as double;
          if(lng>0){
            Map<String, Object> map = {};
            map['lng'] = lng;
            map['lat'] = lat;
              AhaHttp().get(AhaRouter.CityRegeo,params: map).then((result) {
                 print("onLocationChanged=result$result");
                 LocationInfoModel cityModel = LocationInfoModel.fromJson(result) ;
                 locationInfoModel = cityModel;
                 callBack(cityModel);
              });
          }else{
            callBack(null);
          }
        }else if (event['method'] == 'getMaps') {
          hasMaps.clear();
          List <Object?> maps = event['maps'] as  List <Object?>;
          if(maps != null && maps.isNotEmpty){
            for(int i = 0;i<maps.length;i++){
              hasMaps.add(maps[i] as String);
            }
          }
        }
      });
  }

  static showTxt(String msg){
    _methodChannel.invokeMethod('showTxt' ,msg);
  }

  static getMaps(){
    _methodChannel.invokeMethod('getMaps' );
  }

  static buyLine(String msg){
    _methodChannel.invokeMethod('buyLine' ,msg);
  }
  static naviGo(var lat ,var lng, var title ,String type){
    Map<String,dynamic> map = {};
    map['lat'] = lat;
    map['lng'] = lng;
    map['title'] = title;
    map['type'] = type;
    _methodChannel.invokeMethod('naviGo' ,map);
  }

  static addMarker(dynamic arguments){
    _methodChannel.invokeMethod('addMarker' ,arguments);
  }
  static delMarker(dynamic arguments){
    _methodChannel.invokeMethod('delMarker' ,arguments);
  }
  static addMarkers(dynamic arguments){
    _methodChannel.invokeMethod('addMarkers' ,arguments);
  }
  static delMarkers(dynamic arguments){
    _methodChannel.invokeMethod('delMarkers' ,arguments);
  }

}